package com.rede.didiok.admin.support;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rede.didiok.admin.config.BaiduSeoConfig;
import com.rede.didiok.admin.entity.SystemConfigEntity;
import com.rede.didiok.admin.service.AdminService;
import com.rede.didiok.admin.service.SysParamsService;
import com.rede.didiok.admin.service.SystemConfigService;
import com.rede.didiok.admin.utils.FileUtil;
import com.rede.didiok.common.constant.conf.RedisConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.dto.admin.SystemConfigDto;
import com.rede.didiok.common.dto.user.UserDto;
import com.rede.didiok.common.feign.CommunityFeignClient;
import com.rede.didiok.common.feign.FileFeignClient;
import com.rede.didiok.common.feign.RankFeignClient;
import com.rede.didiok.common.feign.UserFeignClient;
import com.rede.didiok.common.utils.JsonUtils;
import com.rede.didiok.common.utils.RedisUtil;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.common.utils.business.DomainEventUtil;
import com.rede.didiok.common.utils.business.RabbitMqUtil;

/**
 * @author 遇见
 */
@Service(value = "userSupport")
@Slf4j
public class UserSupport {

    /**
     * 系统参数服务
     */
    @Resource
    private SysParamsService sysParamsService;
    /**
     * 系统参数服务
     */
    @Resource
    private SystemConfigService systemConfigService;

    /**
     * 榜单服务
     */
    @Resource
    RankFeignClient rankFeignClient;

    /**
     * 社区服务
     */
    @Resource
    CommunityFeignClient communityFeignClient;

    /**
     * 用户服务
     */
    @Resource
    private UserFeignClient userFeignClient;

    /**
     * 管理员服务
     */
    @Resource
    private AdminService adminService;

    @Resource
    private FileUtil fileUtil;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private RabbitMqUtil rabbitMqUtil;

    @Autowired
    FileFeignClient fileFeignClient;

    @Resource
    private BaiduSeoConfig baiduSeoConfig;

    /**
     * 领域事件工具类
     */
    @Resource
    private DomainEventUtil domainEventUtil;


    /**
     * 重置用户密码
     *
     * @param userDto
     * @return
     */
    public String resetUserPassword(UserDto userDto) {
        String defaultPassword = sysParamsService.getSysParamsValueByKey(SysConf.SYS_DEFAULT_PASSWORD);
        if (StringUtils.isNotEmpty(defaultPassword)) {
            userDto.setDefaultPassword(defaultPassword);
        }
        return userFeignClient.resetUserPassword(userDto);
    }

    /**
     * 新增用户
     *
     * @param userDto
     * @return
     */
    public String addUser(UserDto userDto) {
        String defaultPassword = sysParamsService.getSysParamsValueByKey(SysConf.SYS_DEFAULT_PASSWORD);
        if (StringUtils.isNotEmpty(defaultPassword)) {
            userDto.setDefaultPassword(defaultPassword);
        }
        return userFeignClient.addUser(userDto);
    }

    /**
     * 获取用户列表
     *
     * @param userDto
     * @return
     */
    public String getUserPageList(UserDto userDto) {

        // 从Redis中获取系统配置
        String systemConfigJson = redisUtil.get(RedisConf.SYSTEM_CONFIG);
        if (StringUtils.isEmpty(systemConfigJson)) {
            //  通过feign获取SystemConf
            SystemConfigEntity systemConfig = systemConfigService.getOneSystemConfig();
            // 将系统配置存入Redis中【设置过期时间24小时】
            SystemConfigDto systemConfigDto = new SystemConfigDto();
            BeanUtils.copyProperties(systemConfig, systemConfigDto);
            redisUtil.setEx(RedisConf.SYSTEM_CONFIG, JsonUtils.objectToJson(systemConfigDto), 24, TimeUnit.HOURS);
        }
        return userFeignClient.getUserPageList(userDto);
    }
}
